//
//  ViewController.m
//  CAKeyframeAnimationCircule
//
//  Created by Xwoder on 17/1/30.
//  Copyright © 2017年 Xwoder. All rights reserved.
//

#import "ViewController.h"
#import "UIView+ALQuickFrame.h"

#define ViewCenterX (self.view.center.x)
#define ViewCenterY (self.view.center.y)

#define OrangeViewWidth 50
#define OrangeViewHeight 50

@interface ViewController ()

@property (nonatomic, weak) UIView *orangeView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIView *orangeView = [[UIView alloc] init];
    orangeView.backgroundColor = [UIColor orangeColor];
    orangeView.size = CGSizeMake(OrangeViewWidth, OrangeViewHeight);
    CGFloat x = OrangeViewWidth / 2;
    CGFloat y = ViewCenterY;
    orangeView.center = CGPointMake(x, y);
    [self.view addSubview:orangeView];
    self.orangeView = orangeView;
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    // 路径
    CGMutablePathRef path = CGPathCreateMutable();
    CGFloat x = ViewCenterX;
    CGFloat y = ViewCenterY;
    CGFloat radius = ViewCenterX - OrangeViewWidth / 2;
    CGPathAddArc(path, NULL, x, y, radius, -M_PI, M_PI, 0);
    
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    animation.keyPath = @"position";
    animation.path = path;
    animation.duration = 2;
    animation.fillMode = kCAFillModeForwards;
    animation.removedOnCompletion = NO;
    [self.orangeView.layer addAnimation:animation forKey:nil];
}

@end
